package com.sise.rsc.aspect;

import com.alibaba.fastjson.JSON;
import com.sise.rsc.entity.APackage;
import com.sise.rsc.service.PackageService;
import com.sise.rsc.service.TaskService;
import com.sise.rsc.util.CacheUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 切面类，当快递员状态改变为正常时触发
 */
@Aspect
@Component
public class CourierStateAspect {

    @Resource
    private PackageService packageService;
    @Resource
    private TaskService taskService;

    //execution(public * com.example.aop..*.*(..))
    @Pointcut("execution(public * com.sise.rsc.service.CourierService.changStateToWorking(..))")
    public void pointCut(){}

    @After("pointCut()")
    public void doAfter(JoinPoint joinPoint){

        System.out.println("@After(\"pointCut()\")");
        for (String pNo : CacheUtil.packageAutoList) {
            APackage pa = packageService.findAPackageByNo(pNo);
            if (pa!=null){
                //分配任务：
                if (taskService.AssignTasks(pa,"取件")){
                    //成功
                    CacheUtil.packageAutoList.remove(pNo);
                    System.out.println("package:" + pNo+" 成功分配取件快递员");
                }else {
                    //失败,不做处理
                }
            }else {
                //包裹不存在，移除
                CacheUtil.packageAutoList.remove(pNo);
            }
        }

        // 接收到请求，记录请求内容
//        RequestAttributes attributes =  RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = (HttpServletRequest) attributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//
//        Enumeration enumeration = request.getParameterNames();
//        Map<String, String> stringMap = new HashMap<String, String>();
//        while (enumeration.hasMoreElements()){
//            String name = enumeration.nextElement().toString();
//            stringMap.put(name,request.getParameter(name));
//        }
//
//        String json = JSON.toJSONString(stringMap);
//
//        System.out.println(json);


    }
//    @AfterReturning(value = "pointCut()",returning = "reObject")
//    public void doAfterReturning(JoinPoint joinPoint, Object reObject){
//        RequestAttributes attributes =  RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = (HttpServletRequest) attributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//
//        HttpSession session = request.getSession();
//        HttpServletResponse response =((ServletRequestAttributes)attributes).getResponse();
//        if (Integer.valueOf(session.getAttribute("grade").toString()) <= 100){
//            try {
//                response.sendRedirect("/aop/err");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    @AfterThrowing(value = "pointCut()",throwing ="ex" )
//    public ModelAndView doAfterThrowing(Throwable ex){
//
//        ModelAndView modelAndView = new ModelAndView();
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes r = (ServletRequestAttributes) requestAttributes;
//        HttpServletRequest request = r.getRequest();
//        modelAndView.setViewName("error/500");
//        System.err.println(ex.getMessage());
//        return modelAndView;
//
//    }
}
